/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

package com.aptech.struts2.action;

import com.aptech.domain.JPA.Event;
import com.aptech.business.IAccountManager;
import com.aptech.business.IEventManager;
import com.aptech.domain.JPA.Topic;
import com.aptech.domain.model.AccountDO;
import com.aptech.domain.model.EventDO;
import com.aptech.domain.model.EventRequestDO;
import com.aptech.util.AptechUtility;
import com.aptech.util.HibernateUtil;
import com.opensymphony.xwork2.ActionSupport;
import com.opensymphony.xwork2.ModelDriven;
import java.util.ArrayList;
import java.util.List;
import org.apache.log4j.Logger;
import org.hibernate.Criteria;
import org.hibernate.Session;
import org.hibernate.Transaction;
import org.hibernate.criterion.Order;
import org.hibernate.criterion.Restrictions;

/**
 *
 * @author Admin
 */
public class EventCRUDAction extends ActionSupport implements ModelDriven<EventCRUDActionModel> {
    static final Logger logger = Logger.getLogger(EventCRUDAction.class);
    EventCRUDActionModel model = new EventCRUDActionModel();
    private IEventManager eventManager;
    private IAccountManager accountManager;

    /**
     * @return the eventManager
     */
    public IEventManager getEventManager() {
        return eventManager;
    }

    /**
     * @param eventManager the eventManager to set
     */
    public void setEventManager(IEventManager eventManager) {
        this.eventManager = eventManager;
    }

    /**
     * @return the accountManager
     */
    public IAccountManager getAccountManager() {
        return accountManager;
    }

    /**
     * @param accountManager the accountManager to set
     */
    public void setAccountManager(IAccountManager accountManager) {
        this.accountManager = accountManager;
    }

    public EventCRUDActionModel getModel() {
        return model;
    }
    public String executeListAllEvent() {
        try {
            List eventList = eventManager.getAllEvent();
            model.setEventList(eventList);
            return SUCCESS;
        } catch (Exception e) {
            logger.debug("LOCATION", e);
            return ERROR;
        }
    }
    public String executeViewEventDetail() {
        try {
            final String LOCATION = "executeViewEventDetail()";
            /*Event event = model.getEvent() != null ? model.getEvent() : new Event();
            SearchEventCondition searchEventCondition = new SearchEventCondition();
            searchEventCondition.setEvent(model.getEvent());
            System.out.println(event.getEventId());
            event = eventManager.getEventDetails(searchEventCondition);
             *
             */
            
            Transaction tx = null;
            Session session = HibernateUtil.getInstance().openSession();
            tx = session.beginTransaction();
            Criteria ct = session.createCriteria(Topic.class);
            ct.add(Restrictions.like("referenceTypeCode", "EVENT"));
            ct.add(Restrictions.like("referenceId",2));
            ct.addOrder(Order.desc("topicId"));
            List<Topic> topicList = ct.list();
            List<EventDO> eventList = eventManager.searchEvent(model.getSearchEventCondition());
            EventDO event = new EventDO();
            if(eventList != null) {
                event = eventList.get(0);
                List<AccountDO> accountList = new ArrayList<AccountDO>();
                if(event.getEventRequests() != null) {
                    for(EventRequestDO eventRequest : event.getEventRequests()) {
                        accountList.add(eventRequest.getAccount());
                    }
                }
                model.setEvent(event);
                model.setTopicList(topicList);
                model.setAccountList(accountList);
            }            
            return SUCCESS;
        } catch (Exception e) {
            logger.debug("LOCATION", e);
            return ERROR;
        }
    }
    public String executeSaveEvent() {
        try {
            if(model.getEvent() == null) {
                return INPUT;
            } else {
                EventDO event = model.getEvent();
                eventManager.addEvent(event);
            }
        } catch (Exception e) {
            logger.debug("LOCATION", e);
            return ERROR;
        }
        return SUCCESS;
    }
    public String executeJoinEvent() {
        try {
            if(model.getEvent() == null) {
                return ERROR;
            } else {
                Integer eventId = Integer.valueOf(model.getEvent().getEventId());
                if(!AptechUtility.isAnonymous()) {
                    eventManager.saveEventRequest(eventId, AptechUtility.getCurLoginId());
                }
            }
        } catch (Exception e) {
            logger.debug("LOCATION", e);
            return ERROR;
        }
        return SUCCESS;
    }

    public String executeWriteEventComment() {
        try {
            if(model.getTopic() == null) {
                return ERROR;
            } else {
                if(!AptechUtility.isAnonymous()) {
                    int topicId = Integer.valueOf(model.getTopic().getTopicId());
                    String postContent = model.getPost().getPostContent();
                    eventManager.saveCommentForEvent(topicId, postContent);
                }

            }
        } catch (Exception e) {
            logger.debug("LOCATION", e);
            return ERROR;
        }
        return SUCCESS;
    }


    


}
